package com.gmss.crawler.module.patent.service.impl;

import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.module.patent.domain.Patent;
import com.gmss.crawler.module.patent.mapper.PatentMapper;
import com.gmss.crawler.module.patent.service.IPatentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.utils.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 专利表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-10-21
 */
@Service
public class PatentServiceImpl extends ServiceImpl<PatentMapper, Patent> implements IPatentService {

    @Resource
    private PatentMapper patentMapper;


    @Override
    public Patent getPatentInfoByUserName(PersonInfo personInfo) {
        return null;
    }

    /**
     * 查询数据库，查出专利封装到list中
     *
     * @param queryMap
     * @return
     */
    @Override
    public List<Patent> queryExportExcelData(Map<String, Object> queryMap) {
        List<Patent> list = patentMapper.queryExportExcelData(queryMap);
        return list;
    }

    /**
     * 把list中的专利信息写入excel
     *
     * @param filePath
     * @param fileName
     * @param patentList
     */
    @Override
    public boolean writeExcel(String filePath, String fileName, List<Patent> patentList) {
        List<LinkedHashMap<String, Object>> list = convertExcel(patentList);
        return ExcelUtils.saveExcel(fileName, filePath, list);
    }

    /**
     * 把接收的patentList转换成excel需要的形式
     *
     * @param lists
     * @return
     */
    public List<LinkedHashMap<String, Object>> convertExcel(List<?> lists) {
        List<LinkedHashMap<String, Object>> rows = new ArrayList<>();
        Object obj = lists.get(0);
        if (obj instanceof Patent) {
            List<Patent> list = (List<Patent>) lists;
            for (Patent patent : list) {
                LinkedHashMap<String, Object> row = new LinkedHashMap<>();
                row.put("标识", patent.getPersonId());
                row.put("姓名", StringUtils.isNotBlank(patent.getName()) ? patent.getName() : "");
                row.put("头衔", StringUtils.isNotBlank(patent.getAcademicianTitleValue()) ? patent.getAcademicianTitleValue() : "");
                row.put("专利名称", StringUtils.isNotBlank(patent.getPatentName()) ? patent.getPatentName() : "");
                row.put("专利类型", StringUtils.isNotBlank(patent.getPatentType()) ? patent.getPatentType() : "");
                row.put("专利号", StringUtils.isNotBlank(patent.getPatentNum()) ? patent.getPatentNum() : "");
                row.put("申请编号", StringUtils.isNotBlank(patent.getApplyNum()) ? patent.getApplyNum() : "");
                row.put("申请日期", StringUtils.isNotBlank(patent.getApplyDate()) ? patent.getApplyDate() : "");
                row.put("申请单位", StringUtils.isNotBlank(patent.getInstituteStandard()) ? patent.getInstituteStandard() : "");
                row.put("公开公告日", StringUtils.isNotBlank(patent.getPublicAnnouncementDate()) ? patent.getPublicAnnouncementDate() : "");
                row.put("申请人", StringUtils.isNotBlank(patent.getProposer()) ? patent.getProposer() : "");
                row.put("归属行业", StringUtils.isNotBlank(patent.getDomain()) ? patent.getDomain() : "");
                rows.add(row);
            }
        }
        return rows;
    }
}
